<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\Mysql;
$database = Mysql::init();

if (isset($_POST["Password"]) && isset($_POST["newgroup"]) && isset($_SESSION["username"]) && ($_SESSION["username"] != "")) {
    $newgroup = $_POST["newgroup"];
    $Password = $_POST["Password"];
    $username = $_SESSION["username"];
    $oldpassword = $database->select("user_text", ["[>]pass_key" => ["id" => "uid"]], ["salt", "password"], ["username" => $username]);
    $salt = $oldpassword[0]["salt"];
    $oldpassword = $oldpassword[0]["password"];
    $Password = md5($Password);
    $Password = hash("sha512", $Password . $salt);
    $Password = hash("sha512", $Password);
    if ($Password == $oldpassword) {
        $oldname = $database->select("user_group", ["id"], ["groupname" => $newgroup]);
        $oldname = $oldname[0]["id"];
        echo $oldname;
        if ($oldname != null) {
            echo "<script>alert('添加失败,用户组名已存在!');location.href='/admin/lsysg.php';</script>";
        } else {
            $i = $database->count("user_group");
            $i = $i + 1;
            $database->insert("user_group", ["id" => $i, "groupname" => $newgroup]);
            require_once 'getip.php';
            $client_ip = getClientIP();
            $timestamp = time();
            $content = "添加了新组，组名是：".$newgroup;
            $database->insert("secure_info", ["user" => $username, "ip" => $client_ip, "times" => $timestamp, "content" => $content]);
            echo "<script>alert('添加成功!');location.href='/admin/lsysg.php';</script>";
        }
    } else {
        echo "<script>alert('密码错误!');location.href='/admin/lsysg.php';</script>";
    }
}
